package com.fenfen.novel.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fenfen.novel.R;
import com.fenfen.novel.model.Chapter;
import com.fenfen.novel.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class ChapterAdapter extends RecyclerView.Adapter<ChapterAdapter.ChapterHolderView> {

  private static String Tag = ChapterAdapter.class.getSimpleName();

  private Context mContext;
  private List<Chapter> mData = new ArrayList<>();
  private ChapterClickListener mItemClickListener;
  private boolean hasLoadingAll;
  private int currentPos = CommonUtil.NONE_INT_DATA;
  private int colorPrimary, colorAccent, secondaryTextColor;

  public void setHasLoadingAll(boolean hasLoadingAll) {
    this.hasLoadingAll = hasLoadingAll;
  }

  public ChapterAdapter(Context context) {
    this.mContext = context;
    colorPrimary = CommonUtil.resolveColor(mContext, R.attr.colorPrimary);
    colorAccent = CommonUtil.resolveColor(mContext, R.attr.colorAccent);
    secondaryTextColor = CommonUtil.resolveColor(mContext, android.R.attr.textColorSecondary);
  }

  public ChapterAdapter(Context context, List<Chapter> list) {
    this(context);
    setData(list);
  }

  @Override
  public ChapterHolderView onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.novel_chapter_item, parent, false);
    ChapterHolderView holder = new ChapterHolderView(view);
    return holder;
  }

  @Override
  public void onBindViewHolder(ChapterHolderView holder, final int position) {
    if (hasLoadingAll && position == mData.size()) {
      holder.chapterTitle.setText(mContext.getString(R.string.load_all));
      holder.chapterTitle.setGravity(Gravity.CENTER);
      holder.chapterTitle.setTextColor(colorPrimary);
      holder.itemView.setOnClickListener(new ChapterItemClickListener(position, hasLoadingAll));
    } else {
      Chapter chapter = mData.get(position);
      if(currentPos != CommonUtil.NONE_INT_DATA && position == currentPos) {
        holder.chapterTitle.setTextColor(colorAccent);
        holder.itemView.setOnClickListener(null);
      } else {
        holder.chapterTitle.setTextColor(secondaryTextColor);
        holder.itemView.setOnClickListener(new ChapterItemClickListener(position));
      }
      holder.chapterTitle.setText(chapter.getChapterTitle());
      holder.chapterTitle.setTag(chapter.getId());
    }
  }

  public void setCurrentChapter(int pos) {
    if(this.currentPos == pos) {
      return;
    }
    if(this.currentPos != CommonUtil.NONE_INT_DATA) {
      notifyItemChanged(currentPos);
    }
    this.currentPos = pos;
    notifyItemChanged(pos);
  }

  public void setData(List<Chapter> list) {
    if (list != null && list.size() > 0) {
      mData.clear();
      mData.addAll(list);
      notifyDataSetChanged();
    }
  }

  public void addData(List<Chapter> list) {
    if (list != null && list.size() > 0) {
      int positionStart = mData.size();
      mData.addAll(list);
//      notifyDataSetChanged();
      notifyItemRangeInserted(positionStart, list.size());
    }
  }

  @Override
  public int getItemCount() {
    int chapterSize = mData.size();
    if (!hasLoadingAll) {
      return chapterSize;
    } else {
      return chapterSize + 1;
    }
  }

  public List<Chapter> getData() {
    return mData;
  }

  public class ChapterHolderView extends RecyclerView.ViewHolder {
    private TextView chapterTitle;

    public ChapterHolderView(View itemView) {
      super(itemView);
      chapterTitle = (TextView) itemView.findViewById(R.id.chapter_title);
    }
  }

  public void setItemClickListener(ChapterClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
  }

  public interface ChapterClickListener {
    void onItemClick(View v, Chapter chapter);

    void onLoadAllItemClick(View v);
  }

  private class ChapterItemClickListener implements View.OnClickListener {
    private int pos;
    private boolean isLoadingAll;

    public ChapterItemClickListener(int position, boolean hasLoadingAll) {
      this(position);
      this.isLoadingAll = hasLoadingAll;
    }

    public ChapterItemClickListener(int position) {
      this.pos = position;
    }

    @Override
    public void onClick(View v) {
      if (mItemClickListener != null) {
        if (isLoadingAll) {
          mItemClickListener.onLoadAllItemClick(v);
        } else {
          mItemClickListener.onItemClick(v, mData.get(pos));
        }
      }
    }
  }
}
